package cn.wyl.basic.util;

import cn.wyl.basic.jwt.JwtUtils;
import cn.wyl.basic.jwt.LoginData;
import cn.wyl.basic.jwt.Payload;
import cn.wyl.basic.jwt.RsaUtils;
import cn.wyl.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request){
        //获取请求头中的token
        String token = request.getHeader("token");
        if(token != null){
            try {
                //获取公钥
                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader()
                        .getResource("auth_rsa.pub").getFile());
                //实用工具类解密获取载荷PayLoad
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(token,publicKey,LoginData.class);
                //
                return payload.getLoginData().getLogininfo();
            }catch (Exception e){
                return null;
            }
        }
        return null;
    }
}
